package com.bawei.module_mine.audit

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.bawei.lib_common.base.BaseViewModel
import com.bawei.lib_common.base.state.Failed
import com.bawei.lib_common.base.state.Loading
import com.bawei.lib_common.base.state.RequestEvent
import com.bawei.lib_common.base.state.Success
import com.bawei.module_mine.applyfor.ApplyForRepository
import com.bawei.module_mine.model.CarOrderModelItem
import com.bawei.module_mine.model.NewsModelItem
import com.bawei.module_mine.model.VisitOrderModelItem
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.asSharedFlow
import javax.inject.Inject
@HiltViewModel
class AuditViewModel@Inject constructor(
    private val mRepository: AuditRepository
) : BaseViewModel() {
    //车位申请
    private val _parkingEvent = MutableSharedFlow<RequestEvent>()
    val parkingEvent get() = _parkingEvent.asSharedFlow()
    var parkingList: MutableList<CarOrderModelItem> = mutableListOf()

    //访客申请
    private val _visitEvent = MutableSharedFlow<RequestEvent>()
    val visitEvent get() = _visitEvent.asSharedFlow()
    var visitList: MutableList<VisitOrderModelItem> = mutableListOf()

    //公共申请
    private val _newsEvent = MutableSharedFlow<RequestEvent>()
    val newsEvent get() = _visitEvent.asSharedFlow()
    var newsList: MutableList<NewsModelItem> = mutableListOf()
    fun getsParkingList(){
        execute(
            mRepository.parkingApply(),
            onStart = {
                _parkingEvent.emit(Loading)
            },
            onError = {
                _parkingEvent.emit(Failed(it))
            },
            onFinish = {
                parkingList.clear()
                parkingList.addAll(it.data)
                _parkingEvent.emit(Success)
            }
        )
    }
    fun getVisitList(){
        execute(
            mRepository.visitApply(),
            onStart = {
                _visitEvent.emit(Loading)
            },
            onError = {
                _visitEvent.emit(Failed(it))
            },
            onFinish = {
                visitList.clear()
                visitList.addAll(it.data)
                _visitEvent.emit(Success)

            }
        )
    }
    fun getNewsList(){
        execute(
            mRepository.newsApply(),
            onStart = {
                _newsEvent.emit(Loading)
            },
            onError = {
                _newsEvent.emit(Failed(it))
            },
            onFinish = {
                newsList.clear()
                newsList.addAll(it.data)
                _newsEvent.emit(Success)
            }
        )
    }
}